package com.xuecheng.media.controller;

import com.xuecheng.commons.model.dto.MediaDto;
import com.xuecheng.commons.model.dto.MediaQueryDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.VodUploadDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.media.domain.Media;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 媒资信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private MediaService  mediaService;

    //http://manage.xuecheng.com/api/media/media/vod-token

    @PostMapping("/vod-token")
    public ResponseResult vodToken(@RequestBody VodUploadDto dto) throws Exception {
        return mediaService.vodToken(dto);
    }

    @GetMapping("/refresh-vod-token/{videoId}")
    public ResponseResult refreshVodToken(@PathVariable("videoId")String videoId) throws Exception {
        return mediaService.refreshVodToken(videoId);
    }

    @PostMapping
    public ResponseResult saveMedia(@RequestBody MediaDto dto) throws Exception {
        return mediaService.saveMedia(dto);
    }

    @PostMapping("/list")
    public ResponseResult list(PageRequest pageRequest,@RequestBody MediaQueryDto dto){
        return mediaService.findByList(pageRequest,dto);
    }

    @GetMapping("/preview/{mediaId}")
    public ResponseResult preview(@PathVariable("mediaId")Long mediaId) throws Exception {
        return mediaService.preview(mediaId);
    }

    @DeleteMapping("/{mediaId}")
    public ResponseResult deleteMedia(@PathVariable("mediaId")Long mediaId) throws Exception {
        return mediaService.deleteMedia(mediaId);
    }

    @GetMapping("/{mediaId}")
    public ResponseResult<MediaVo> findMediaById(@PathVariable("mediaId")Long mediaId){
        Media media = mediaService.getById(mediaId);
        MediaVo mediaVo = BeanHelper.copyProperties(media, MediaVo.class);
        return ResponseResult.okResult(mediaVo);
    }
}
